﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace DreamExplanation
{
    public partial class DreamContent : PhoneApplicationPage
    {
        public DreamContent()
        {
            InitializeComponent();
        }

        private string categoryName = null;
        private string contentSearch = null;
        private string subcategoryName = null;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            //retrieve pass in parameter
            categoryName = NavigationContext.QueryString["categoryName"];
            subcategoryName = NavigationContext.QueryString["subCategoryName"];
            contentSearch = NavigationContext.QueryString["contentSearch"];

            //generate binding data
            var dreamCategory = (from car in App.dreamCategories.Elements("DreamCategory")
                                 where car.Element("Name") != null
                                 && car.Element("Name").Value == categoryName
                                 select car).FirstOrDefault();

            if (dreamCategory == null)
                return;

            var dreamContent = (from dream in dreamCategory.Element("Dreams").Elements("DreamEntity")
                               where dream.Element("Title")!=null
                               && dream.Element("Title").Value == subcategoryName
                               && dream.Element("Content")!=null
                               select dream).FirstOrDefault();
            if (dreamContent != null
                && dreamContent.Element("Content")!=null
                && !string.IsNullOrEmpty(dreamContent.Element("Content").Value))
            {
                tbDreamContent.Text = dreamContent.Element("Content").Value;

                //contentSearch
                Color color = Color.FromArgb(255, 254, 247, 71);

                if (!string.IsNullOrEmpty(contentSearch))
                {
                    Utility.BoldText(ref tbDreamContent, contentSearch, color);
                }

            }

            if (dreamContent != null
                && dreamContent.Element("RelativedSubCategory") != null
                && dreamContent.Element("RelativedSubCategory").Elements("string") !=null
                && dreamContent.Element("RelativedSubCategory").Elements("string").Count()>0)
            {
                var relativeList = from o in dreamContent.Element("RelativedSubCategory").Elements("string")
                           where string.IsNullOrEmpty(o.Value) == false
                           select o.Value;
                lbxCarmakers.ItemsSource = relativeList;
            }

            //set page title
            if (dreamContent != null
                && dreamContent.Element("Navigation") != null
                && !string.IsNullOrEmpty(dreamContent.Element("Navigation").Value))
            {
                PageTitle.Text = dreamContent.Element("Navigation").Value;
            }
        }

        private void lbxCarmakers_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (e != null
                && e.AddedItems != null
                && e.AddedItems.Count > 0
                && e.AddedItems[0] != null
                && e.AddedItems[0] is string)
            {
                string subCategory = e.AddedItems[0] as string;
                if (App.navigationInfos != null
                    && App.navigationInfos.Count() >0)
                {
                    var navigationInfo = (from o in App.navigationInfos
                                          where o.CategoryName == categoryName && o.SubCategoryName == subCategory
                                          select o).FirstOrDefault();
                    string uri = "/MainPage.xaml";
                    if (navigationInfo != null
                        && !string.IsNullOrEmpty(navigationInfo.CategoryName)
                        && !string.IsNullOrEmpty(navigationInfo.SubCategoryName))
                    {
                        //if find sub category, return user result
                        uri = string.Format("/Dreamcontent.xaml?categoryName={0}&&subCategoryName={1}&&contentSearch={2}", categoryName, e.AddedItems[0], "");
                        NavigationService.Navigate(new Uri(uri, UriKind.Relative));
                    }
                    else
                    {
                        //if no find sub category, to search content
                        uri = string.Format("/SubCategoryPage.xaml?categoryName={0}&&subCategoryName={1}&&contentSearch={2}", "", "", e.AddedItems[0]);
                        NavigationService.Navigate(new Uri(uri, UriKind.Relative));
                    }

                }
                
            }
        }
    }
}